package demoMod.invincibleOne.skins;

import basemod.BaseMod;
import com.badlogic.gdx.graphics.Texture;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.GameActionManager;
import com.megacrit.cardcrawl.actions.animations.VFXAction;
import com.megacrit.cardcrawl.actions.common.*;
import com.megacrit.cardcrawl.actions.unique.RemoveAllPowersAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.DamageInfo;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.dungeons.Exordium;
import com.megacrit.cardcrawl.dungeons.TheBeyond;
import com.megacrit.cardcrawl.dungeons.TheCity;
import com.megacrit.cardcrawl.events.beyond.SensoryStone;
import com.megacrit.cardcrawl.monsters.AbstractMonster;
import com.megacrit.cardcrawl.powers.VulnerablePower;
import com.megacrit.cardcrawl.powers.WeakPower;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.MonsterRoomBoss;
import com.megacrit.cardcrawl.rooms.TreasureRoomBoss;
import com.megacrit.cardcrawl.shop.ShopScreen;
import com.megacrit.cardcrawl.vfx.cardManip.ShowCardAndObtainEffect;
import demoMod.invincibleOne.InvincibleOneMod;
import demoMod.invincibleOne.cards.invincible.*;
import demoMod.invincibleOne.enums.CardTagsEnum;
import demoMod.invincibleOne.patches.CharacterSelectPatch;
import demoMod.invincibleOne.powers.DoomPower;
import demoMod.invincibleOne.powers.InvinciblePower2;
import demoMod.invincibleOne.relics.CostOfBlessing2Artifact;
import demoMod.invincibleOne.relics.HeavenScepter;
import demoMod.invincibleOne.relics.SecondWindRelic;
import demoMod.invincibleOne.relics.TaskGoal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static demoMod.invincibleOne.InvincibleOneMod.getResourcePath;

public class PrimeSkin extends Skin {
    public PrimeSkin() {
        super(3);
        isPrimeForm = true;
    }

    @Override
    public void load() {
        portrait = new Texture(getResourcePath("charSelect/portrait3.png"));
        shoulder1 = new Texture(InvincibleOneMod.getResourcePath("char/shoulder1_1.png"));
        shoulder2 = new Texture(InvincibleOneMod.getResourcePath("char/shoulder1_2.png"));
        sprite = new Texture(InvincibleOneMod.getResourcePath("char/character3.png"));
        for (int i = 0; i < 3; i++) {
            deathCG[i] = new Texture(getResourcePath("char/victory1_" + (i+1) + ".png"));
        }
    }

    @Override
    public void onEnterRoom(AbstractRoom room) {
        if (room instanceof TreasureRoomBoss && AbstractDungeon.id.equals(TheCity.ID)) {
            if (AbstractDungeon.player.hasRelic(SecondWindRelic.ID) && AbstractDungeon.player.hasRelic(HeavenScepter.ID)) {
                AbstractRelic r = AbstractDungeon.player.getRelic(SecondWindRelic.ID);
                if (r.counter > 0) {
                    int doom = AbstractDungeon.player.currentHealth / 3;
                    AbstractRelic hs = AbstractDungeon.player.getRelic(HeavenScepter.ID);
                    hs.counter += doom;
                    hs.flash();
                }
            }
        }
    }

    @Override
    public void atTurnStart() {
        AbstractPlayer p = AbstractDungeon.player;

        AbstractDungeon.actionManager.addToBottom(new AbstractGameAction() {
            @Override
            public void update() {
                List<AbstractCard> cards = p.drawPile.group.stream().filter(c -> (c.type == AbstractCard.CardType.CURSE || c.hasTag(CardTagsEnum.VIRUS))).collect(Collectors.toList());
                if (!cards.isEmpty()) {
                    AbstractCard card = cards.remove(AbstractDungeon.cardRandomRng.random(cards.size() - 1));
                    addToTop(new ExhaustSpecificCardAction(card, p.drawPile, true));
                }
                isDone = true;
            }
        });

        AbstractRelic reborn = p.getRelic(SecondWindRelic.ID);
        int canReborn = reborn == null ? 0 : reborn.counter;
        if (TaskGoal.combatTurn % 3 == 0) {
            AbstractCard card = new Brutal();
            if (AbstractDungeon.id.equals(TheBeyond.ID) || canReborn == 0) {
                card.upgrade();
            }
            AbstractDungeon.actionManager.addToBottom(new MakeTempCardInHandAction(
                    card, 1
            ));
        }

        if (canReborn == 0 || AbstractDungeon.id.equals(TheBeyond.ID)) {
            AbstractDungeon.player.getRelic(HeavenScepter.ID).counter += 3;
            AbstractDungeon.actionManager.addToBottom(new ApplyPowerAction(p, p, new DoomPower(p, 3)));

            if (TaskGoal.combatTurn == 1) {
                AbstractDungeon.actionManager.addToBottom(new ApplyPowerAction(p, p, new InvinciblePower2(p, 40)));
                for (AbstractMonster m: AbstractDungeon.getMonsters().monsters) {
                    if (!m.isDeadOrEscaped()) {
                        AbstractDungeon.actionManager.addToBottom(new ApplyPowerAction(m, p, new VulnerablePower(m, 99, false)));
                        AbstractDungeon.actionManager.addToBottom(new ApplyPowerAction(m, p, new WeakPower(m, 99, false)));
                    }
                }
            }
        }
    }

    @Override
    public void onGameStart() {
        new HeavenScepter().instantObtain();
        new CostOfBlessing2Artifact().instantObtain();
        new CostOfBlessing2Artifact().instantObtain();
        ShopScreen.purgeCost = 100;
    }

    @Override
    public float onReborn() {
        if (AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMBAT) {
            AbstractDungeon.actionManager.addToBottom(new AbstractGameAction() {
                @Override
                public void update() {
                    (AbstractDungeon.getCurrRoom()).cannotLose = false;
                    for (AbstractMonster m2 : AbstractDungeon.getMonsters().monsters.stream().filter(m2 -> !m2.isDeadOrEscaped() || m2.halfDead).collect(Collectors.toList())) {
                        addToBot(new RemoveAllPowersAction(m2, false));
                        addToBot(new AbstractGameAction() {
                            @Override
                            public void update() {
                                m2.currentHealth = 0;
                                m2.healthBarUpdatedEvent();
                                m2.die();
                                isDone = true;
                            }
                        });
                    }
                    addToBot(new AbstractGameAction() {
                        @Override
                        public void update() {
                            AbstractDungeon.getCurrRoom().endBattle();
                        }
                    });
                    isDone = true;
                }
            });
        }
        return 1.0F;
    }

    @Override
    public int onAttackedToChangeDamage(DamageInfo dmg, int amount){
        if (dmg.type != DamageInfo.DamageType.NORMAL && amount > 0 && AbstractDungeon.player.currentBlock > 1) {
            int reduce = Math.min(amount, AbstractDungeon.player.currentBlock / 2);
            amount -= reduce;
            AbstractDungeon.player.loseBlock(reduce * 2);
        }
        return amount;
    }
}
